package com.example.common.aop;

import com.example.common.annotation.SysLog;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.example.common.constant.MqQueueNameConstant;
import java.lang.reflect.Method;

@Aspect
@Component
@Slf4j
public class SysLogAspect {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Pointcut("@annotation(com.example.common.annotation.SysLog)")
    public void logPointCut() {
    }

    @Around("logPointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable, InterruptedException {
        System.out.println("=============================");
        Object result = point.proceed();
        MethodSignature signature = (MethodSignature) point.getSignature();
        Method method = signature.getMethod();
        String name = method.getName();
        System.out.println("Method Name:" + name);//输出方法名
        SysLog sysLog = method.getAnnotation(SysLog.class);
        System.out.println("Log Value:" + sysLog.toString());//输出注解里面的值
        System.out.println("+++++++++++++++++++++++++++++");
        //  发消息队列
        long start = System.currentTimeMillis();
        rabbitTemplate.convertAndSend(MqQueueNameConstant.SYS_LOG_QUEUE,name);
        log.info("发送mq消息耗时：{}",System.currentTimeMillis()-start);
        return result;
    }
}
